//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "DiscretizedVanillaOption.h"
using namespace Cephei::QL::Pricingengines::Vanilla;
#include <gen/QL/StochasticProcess.h>
#include <gen/QL/TimeGrid.h>
#include <gen/QL/Lattice.h>
#include <gen/QL/Math/Array.h>
#include <gen/QL/DiscretizedAsset.h>
using namespace Cephei::QL;
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Pricingengines::Vanilla::CDiscretizedVanillaOption::CDiscretizedVanillaOption (boost::shared_ptr<QuantLib::DiscretizedVanillaOption>& childNative, Object^ owner) : CDiscretizedAsset(CDiscretizedVanillaOption::typeid)
{
#ifdef HANDLE
	_phDiscretizedVanillaOption = NULL;
#endif
	_ppDiscretizedVanillaOption = &childNative;
    _ppDiscretizedAsset = new boost::shared_ptr<QuantLib::DiscretizedAsset> (boost::dynamic_pointer_cast<QuantLib::DiscretizedAsset> (*_ppDiscretizedVanillaOption));
}
Cephei::QL::Pricingengines::Vanilla::CDiscretizedVanillaOption::CDiscretizedVanillaOption (QuantLib::DiscretizedVanillaOption& childNative, Object^ owner) : CDiscretizedAsset(CDiscretizedVanillaOption::typeid)
{
#ifdef HANDLE
	_phDiscretizedVanillaOption = NULL;
#endif
	_ppDiscretizedVanillaOption = new boost::shared_ptr<QuantLib::DiscretizedVanillaOption> (&childNative);
    _ppDiscretizedAsset = new boost::shared_ptr<QuantLib::DiscretizedAsset> (boost::dynamic_pointer_cast<QuantLib::DiscretizedAsset> (*_ppDiscretizedVanillaOption));
    _DiscretizedVanillaOptionOwner = owner;
    _DiscretizedAssetOwner = owner;
}

Cephei::QL::Pricingengines::Vanilla::CDiscretizedVanillaOption::CDiscretizedVanillaOption (CDiscretizedVanillaOption^ copy) : CDiscretizedAsset(CDiscretizedVanillaOption::typeid)
{
#ifdef HANDLE
	_phDiscretizedVanillaOption = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppDiscretizedVanillaOption = new boost::shared_ptr<QuantLib::DiscretizedVanillaOption> (copy->GetShared());
        _ppDiscretizedAsset = new boost::shared_ptr<QuantLib::DiscretizedAsset> (boost::dynamic_pointer_cast<QuantLib::DiscretizedAsset> (*_ppDiscretizedVanillaOption));
    }
}
Cephei::QL::Pricingengines::Vanilla::CDiscretizedVanillaOption::CDiscretizedVanillaOption (System::Type^ t) : CDiscretizedAsset(CDiscretizedVanillaOption::typeid)
{
#ifdef HANDLE
	_phDiscretizedVanillaOption = NULL;
#endif
	if (!t->IsSubclassOf(CDiscretizedVanillaOption::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Pricingengines::Vanilla::CDiscretizedVanillaOption::CDiscretizedVanillaOption (QuantLib::Handle<QuantLib::DiscretizedVanillaOption>& childNative, Object^ owner)  : CDiscretizedAsset(CDiscretizedVanillaOption::typeid)
{
	_phDiscretizedVanillaOption = &childNative;
	_ppDiscretizedVanillaOption = &static_cast<boost::shared_ptr<QuantLib::DiscretizedVanillaOption>>(childNative.currentLink());
    _ppDiscretizedAsset = new boost::shared_ptr<QuantLib::DiscretizedAsset> (boost::dynamic_pointer_cast<QuantLib::DiscretizedAsset> (*_ppDiscretizedVanillaOption));
    _DiscretizedVanillaOptionOwner = owner;
}
Cephei::QL::Pricingengines::Vanilla::CDiscretizedVanillaOption::CDiscretizedVanillaOption (QuantLib::Handle<QuantLib::DiscretizedVanillaOption> childNative)  : CDiscretizedAsset(CDiscretizedVanillaOption::typeid)
{
	_phDiscretizedVanillaOption = &childNative;
	_ppDiscretizedVanillaOption = &static_cast<boost::shared_ptr<QuantLib::DiscretizedVanillaOption>>(childNative.currentLink());
    _ppDiscretizedAsset = new boost::shared_ptr<QuantLib::DiscretizedAsset> (boost::dynamic_pointer_cast<QuantLib::DiscretizedAsset> (*_ppDiscretizedVanillaOption));
}
#endif
#ifdef STRUCT
Cephei::QL::Pricingengines::Vanilla::CDiscretizedVanillaOption::CDiscretizedVanillaOption (QuantLib::DiscretizedVanillaOption childNative)  : CDiscretizedAsset(CDiscretizedVanillaOption::typeid)
{
#ifdef HANDLE
	_phDiscretizedVanillaOption = NULL;
#endif
	_ppDiscretizedVanillaOption = new boost::shared_ptr<QuantLib::DiscretizedVanillaOption> (new QuantLib::DiscretizedVanillaOption (childNative));
    _ppDiscretizedAsset = new boost::shared_ptr<QuantLib::DiscretizedAsset> (boost::dynamic_pointer_cast<QuantLib::DiscretizedAsset> (*_ppDiscretizedVanillaOption));
}
#endif

Cephei::QL::Pricingengines::Vanilla::CDiscretizedVanillaOption::~CDiscretizedVanillaOption ()
{
    if (_ppDiscretizedVanillaOption != NULL)
    {
	    delete _ppDiscretizedVanillaOption;
        _ppDiscretizedVanillaOption = NULL;
    }
}
Cephei::QL::Pricingengines::Vanilla::CDiscretizedVanillaOption::!CDiscretizedVanillaOption ()
{
    if (_ppDiscretizedVanillaOption != NULL)
    {
	    delete _ppDiscretizedVanillaOption;
    }
}
QuantLib::DiscretizedVanillaOption& Cephei::QL::Pricingengines::Vanilla::CDiscretizedVanillaOption::GetReference ()
{
    if (_ppDiscretizedVanillaOption == NULL) throw gcnew NativeNullException ();
	return **_ppDiscretizedVanillaOption;
}
boost::shared_ptr<QuantLib::DiscretizedVanillaOption>& Cephei::QL::Pricingengines::Vanilla::CDiscretizedVanillaOption::GetShared ()
{
    if (_ppDiscretizedVanillaOption == NULL) throw gcnew NativeNullException ();
	return *_ppDiscretizedVanillaOption;
}
QuantLib::DiscretizedVanillaOption* Cephei::QL::Pricingengines::Vanilla::CDiscretizedVanillaOption::GetPointer ()
{
    if (_ppDiscretizedVanillaOption == NULL) throw gcnew NativeNullException ();
	return &**_ppDiscretizedVanillaOption;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::DiscretizedVanillaOption>& Cephei::QL::Pricingengines::Vanilla::CDiscretizedVanillaOption::GetHandle ()
{
	if (_phDiscretizedVanillaOption == NULL)
	{
		_phDiscretizedVanillaOption = new Handle<QuantLib::DiscretizedVanillaOption> (*_ppDiscretizedVanillaOption);
	}
	return *_phDiscretizedVanillaOption;
}
#endif
bool Cephei::QL::Pricingengines::Vanilla::CDiscretizedVanillaOption::HasNative () 
{
	return (_ppDiscretizedVanillaOption != NULL);
}

Cephei::IVector<Double>^ Cephei::QL::Pricingengines::Vanilla::CDiscretizedVanillaOption::MandatoryTimes::get ()
{
    try
    {
    	std::vector<QuantLib::Time> _rv = (std::vector<QuantLib::Time>)(*_ppDiscretizedVanillaOption)->mandatoryTimes ( );   
        Cephei::IVector<Double>^ _nrv = gcnew CoVector<Double> (gcnew CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Pricingengines::Vanilla::IDiscretizedVanillaOption^ Cephei::QL::Pricingengines::Vanilla::CDiscretizedVanillaOption::Reset (UInt64 size)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Size _size = (QuantLib::Size)ValueHelper::Convert (size);
    	(*_ppDiscretizedVanillaOption)->reset ( _size );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

